package org.wlw.web.service.impl;

import org.wlw.web.anno.Resource;
import org.wlw.web.dao.IColumnDao;
import org.wlw.web.dao.ITopicDao;
import org.wlw.web.models.Column;
import org.wlw.web.models.Topic;
import org.wlw.web.models.WebException;
import org.wlw.web.service.IColumnService;

import java.util.List;

public class ColumnService extends BaseService implements IColumnService {
    private IColumnDao columnDao;

    private ITopicDao topicDao;

    @Resource
    public void setTopicDao(ITopicDao topicDao) {
        this.topicDao = topicDao;
    }

    @Resource
    public void setColumnDao(IColumnDao columnDao) {
        this.columnDao = columnDao;
    }

    @Override
    public void add(Column t) {
        columnDao.add(t);
    }

    @Override
    public void update(Column t) {
        columnDao.update(t);
    }

    @Override
    public void delete(int id) {
        Column c = new Column();
        c.setPid(id);
        List<Column> cs = columnDao.find(c);
        if(cs!=null&&cs.size()>0) {
            throw new WebException("删除的栏目还有子栏目不能删除!");
        }

        Topic t = new Topic();
        t.setCid(id);
        List<Topic> ts = topicDao.find(t);
        if(ts!=null&&ts.size()>0) {
            throw new WebException("删除的栏目还有文章，不能删除!");
        }
        
        columnDao.delete(id);
    }

    @Override
    public Column load(int id) {
        return columnDao.load(id);
    }

    @Override
    public List<Column> find(Column column) {
        return columnDao.find(column);
    }

    @Override
    public List<Column> findCanAddTopic() {
        return columnDao.findCanAddTopic();
    }
}
